using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace AdHocReports
{
    public partial class step4 : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            MasterPage.NextEnabled = true;
            MasterPage.BackEnabled = true;
            MasterPage.CancelEnabled = true;
            MasterPage.NextClicked += new EventHandler(mp_NextClicked);
            MasterPage.BackClicked += new EventHandler(mp_BackClicked);
            MasterPage.CancelClicked += new EventHandler(mp_CancelClicked);
            MasterPage.ContentTitle = "Create a Report: Group By";

            if (!IsPostBack && !IsCallback)
            {
                // Create a connection object
                using (System.Data.OleDb.OleDbConnection oleConn = new System.Data.OleDb.OleDbConnection(ReportDefinition.ConnectionString))
                {
                    // Create a data table
                    using (System.Data.DataTable dt = new System.Data.DataTable())
                    {
                        // Create a data adapter with column info
                        using (System.Data.OleDb.OleDbDataAdapter oleDA = new System.Data.OleDb.OleDbDataAdapter("SELECT * FROM \"" + ReportDefinition.View + "\" WHERE 0=1", oleConn))
                        {
                            // Fill the data table
                            oleDA.Fill(dt);
                        }

                        for (int i = 0; i < dt.Columns.Count; i++)
                        {
                            ReportDefinition.DataTypes.Add(new Column(dt.Columns[i].ColumnName, dt.Columns[i].DataType.ToString()));
                        }
                    }
                }

                // Loop through the selected columns
                for (int i = 0; i < ReportDefinition.SelectColumns.Count; i++)
                {
                    // If the "select" column is not in the "group" column
                    if (ReportDefinition.GroupColumns.IndexOf(ReportDefinition.SelectColumns[i]) == -1)
                    {
                        // Create a new list item to match the current item
                        System.Web.UI.WebControls.ListItem li = new System.Web.UI.WebControls.ListItem(ReportDefinition.SelectColumns[i], ReportDefinition.SelectColumns[i]);
                        // Add it to the "Available Columns" list
                        lstCols.Items.Add(li);
                    }
                }

                lstGroupBy.Items.Clear();

                foreach (string groupColumn in ReportDefinition.GroupColumns)
                {
                    lstGroupBy.Items.Add(new ListItem(groupColumn, groupColumn));
                }
            }
        }

        void mp_CancelClicked(object sender, EventArgs e)
        {
            Response.Redirect("~/");
        }

        void mp_BackClicked(object sender, EventArgs e)
        {
            // Update the group columns
            UpdateGroupColumns();

            Response.Redirect("step3.aspx");
        }

        void mp_NextClicked(object sender, EventArgs e)
        {
            // Update the group columns
            UpdateGroupColumns();

            if (ReportDefinition.GroupColumns.Count > 0)
            {
                // Go to the next step
                Response.Redirect("step5.aspx");
            }
            else
            {
                // Skip group aggregates
                Response.Redirect("step6.aspx");
            }
        }

        private void UpdateGroupColumns()
        {
            // Empty the list
            ReportDefinition.GroupColumns.Clear();

            // If there are columns selected
            if (lstGroupBy.Items.Count > 0)
            {
                for (int i = 0; i < lstGroupBy.Items.Count; i++)
                {
                    ReportDefinition.GroupColumns.Add(lstGroupBy.Items[i].Value);
                }
            }
        }

        protected void cmdMoveRight_Click(object sender, EventArgs e)
        {
            // Loop through the available columns
            for (int i = 0; i < lstCols.Items.Count; i++)
            {
                // If the current item is selected
                if (lstCols.Items[i].Selected == true)
                {
                    // Add it to the list of group columns
                    lstGroupBy.Items.Add(lstCols.Items[i]);
                }
            }

            // Loop backwards through the available columns
            for (int i = (lstCols.Items.Count - 1); i >= 0; i--)
            {
                // If the current item is selected
                if (lstCols.Items[i].Selected == true)
                {
                    // Remove the item from the available columns
                    lstCols.Items.Remove(lstCols.Items[i]);
                }
            }

            // Loop through the group columns
            for (int i = 0; i < lstGroupBy.Items.Count; i++)
            {
                // Ensure the item is not selected
                lstGroupBy.Items[i].Selected = false;
            }

            // Update the group columns
            UpdateGroupColumns();
        }

        protected void cmdMoveLeft_Click(object sender, EventArgs e)
        {
            // Loop backwards through the group columns
            for (int i = (lstGroupBy.Items.Count - 1); i >= 0; i--)
            {
                // If the current item is selected
                if (lstGroupBy.Items[i].Selected == true)
                {
                    // Add the item back to the available columns
                    lstCols.Items.Add(lstGroupBy.Items[i]);
                    // Remove the item from the group columns
                    lstGroupBy.Items.Remove(lstGroupBy.Items[i]);
                }
            }

            // Loop through the available columns
            for (int i = 0; i < lstCols.Items.Count; i++)
            {
                // Ensure the item is not selected
                lstCols.Items[i].Selected = false;
            }

            // Update the group columns
            UpdateGroupColumns();
        }

        protected void cmdMoveUp_Click(object sender, EventArgs e)
        {
            // Loop through the list items (ignoring the top element)
            for (int i = 1; i < lstGroupBy.Items.Count; i++)
            {
                // If the current item is selected
                if (lstGroupBy.Items[i].Selected == true)
                {
                    // Create a new list item, matching the selected list item
                    System.Web.UI.WebControls.ListItem liBottom = lstGroupBy.Items[i];
                    // Remove the selected item
                    lstGroupBy.Items.Remove(lstGroupBy.Items[i]);
                    // Insert the same element one index up
                    lstGroupBy.Items.Insert(i - 1, liBottom);
                    // Stop after processing the first "swap"
                    break;
                }
            }

            // Update the group columns
            UpdateGroupColumns();
        }

        protected void cmdMoveDown_Click(object sender, EventArgs e)
        {
            // Loop backwards through the loop, ignoring the bottom element
            for (int i = (lstGroupBy.Items.Count - 2); i >= 0; i--)
            {
                // If the current item is selected
                if (lstGroupBy.Items[i].Selected == true)
                {
                    // Create a new list item, matching the selected item
                    System.Web.UI.WebControls.ListItem liTop = lstGroupBy.Items[i];
                    // Remove the current item
                    lstGroupBy.Items.Remove(lstGroupBy.Items[i]);
                    // Add the item one index down
                    lstGroupBy.Items.Insert(i + 1, liTop);
                    // Stop processing the first "swap"
                    break;
                }
            }

            // Update the group columns
            UpdateGroupColumns();
        }
    }
}